from django.db import models


class Book(models.Model):
    title = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    pub_date = models.DateField(auto_now_add=True)

    # 元类信息 : 修改表名
    class Meta:
        db_table = 'book'


# 主页图片地址
class Banner(models.Model):
    carousel_id = models.IntegerField(auto_created=True, primary_key=True)
    carousel_url = models.CharField(max_length=100)
    redirect_url = models.CharField(max_length=100)
    carousel_rank = models.IntegerField()
    is_deleted = models.IntegerField(default=0)
    create_time = models.DateField(auto_now_add=True)

    class Meta:
        db_table = 'mall_carousel'


# 用户信息
class MallUser(models.Model):
    user_id = models.IntegerField(primary_key=True)
    phone_num = models.IntegerField(max_length=11)
    login_name = models.CharField(max_length=100)
    user_pwd = models.CharField(max_length=128)
    nick_name = models.CharField(max_length=100)
    locked = models.IntegerField(default=0)
    introduce = models.CharField(max_length=220)
    is_deleted = models.IntegerField(default=0)
    create_time = models.DateField(auto_now_add=True)

    class Meta:
        db_table = 'mall_user'


# 收货人地址
'''如果一个模型使用了外键。那么在对方那个模型被删掉后，该进行什么样的操作。可以通过on_delete来指定。可以指定的类型如下：
1.CASCADE：级联操作。如果外键对应的那条数据被删除了，那么这条数据也会被删除。
2.PROTECT：受保护。即只要这条数据引用了外键的那条数据，那么就不能删除外键的那条数据。
3.SET_NULL：设置为空。如果外键的那条数据被删除了，那么在本条数据上就将这个字段设置为空。如果设置这个选项，前提是要指定这个字段可以为空。
4.SET_DEFAULT：设置默认值。如果外键的那条数据被删除了，那么本条数据上就将这个字段设置为默认值。如果设置这个选项，前提是要指定这个字段一个默认值。
5.SET()：如果外键的那条数据被删除了。那么将会获取SET函数中的值来作为这个外键的值。SET函数可以接收一个可以调用的对象（比如函数或者方法），如果是可以调用的对象，那么会将这个对象调用后的结果作为值返回回去。
6.DO_NOTHING：不采取任何行为。一切全看数据库级别的约束。
以上这些选项只是Django级别的，数据级别依旧是RESTRICT！'''
class UserAddress(models.Model):
    address_id = models.IntegerField(auto_created=True, primary_key=True)
    user_id = models.ForeignKey('MallUser', on_delete=models.CASCADE)
    consignee = models.CharField(max_length=100)
    consignee_phone = models.IntegerField(max_length=11)
    default_flag = models.IntegerField(default=0)
    province_name = models.CharField(max_length=25)
    city_name = models.CharField(max_length=25)
    region_name = models.CharField(max_length=25)
    detail_address = models.CharField(max_length=150)
    create_time = models.DateField(auto_now_add=True)
    update_time = models.DateField(auto_now=True)

    class Meta:
        db_table = 'user_address'


# 用户token
# class UserToken(models.Model):
#     user_id = models.IntegerField(max_length=100)
#     token = models.CharField(max_length=150)
#     update_time = models.DateField(auto_now=True)
#     expire_time =

# 商品信息表
class GoodsInfo(models.Model):
    SellStatus = (
        ('0', '上架'),
        ('1', '下架'),
    )
    goods_id = models.IntegerField(auto_created=True, primary_key=True)
    goods_name = models.CharField(max_length=100)
    goods_intro = models.CharField(max_length=200)
    goods_category_id = models.IntegerField(default=0)
    goods_cover_img = models.CharField(max_length=150)
    goods_carousel = models.CharField(max_length=150)
    goods_detail_content = models.CharField(max_length=150)
    original_price = models.FloatField()
    selling_price = models.FloatField()
    stock_num = models.IntegerField()
    tag = models.CharField(max_length=100, default=0)
    goods_sell_status = models.IntegerField(choices=SellStatus, default=0)
    create_user = models.IntegerField(default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    update_user = models.IntegerField(default=0)
    update_time = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'goods_info'


# 商品分类表
class GoodsCategory(models.Model):
    category_id = models.IntegerField(primary_key=True, auto_created=True)
    category_level = models.CharField(max_length=20)
    parent_id = models.IntegerField(max_length=100)
    category_name = models.CharField(max_length=150)
    category_rank = models.IntegerField()
    is_deleted = models.IntegerField(default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    create_user = models.IntegerField(default=0)
    update_time = models.DateTimeField(auto_now=True)
    update_user = models.IntegerField(default=0)

    class Meta:
        db_table = 'goods_category'


# 购物车表
class ShoppingCart(models.Model):
    cart_item_id = models.IntegerField(primary_key=True, auto_created=True)
    user_id = models.ForeignKey('MallUser', on_delete=models.CASCADE)
    goods_id = models.ForeignKey('GoodsInfo', on_delete=models.CASCADE)
    goods_count = models.IntegerField(max_length=150)
    unit_price = models.FloatField()
    is_deleted = models.IntegerField(default=0)
    create_time = models.DateField(auto_now_add=True)
    update_time = models.DateField(auto_now=True)

    class Meta:
        db_table = 'shopping_cart'


# 订单表
class MallOrder(models.Model):
    # order_id = models.IntegerField(primary_key=True, auto_created=True)
    order_nu = models.IntegerField(primary_key=True)
    user_id = models.ForeignKey('MallUser', on_delete=models.CASCADE)
    total_price = models.FloatField()
    pay_status = models.CharField(max_length=20)
    pay_type = models.CharField(max_length=20)
    pay_time = models.DateTimeField(auto_now=True)
    order_status = models.CharField(max_length=20)
    extra_info = models.CharField(default='', max_length=200)
    is_deleted = models.IntegerField(default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'mall_order'


# 订单项表
class OrderItem(models.Model):
    order_item_id = models.IntegerField(primary_key=True, auto_created=True)
    order_nu = models.ForeignKey('MallOrder', on_delete=models.CASCADE)
    goods_id = models.ForeignKey('GoodsInfo', on_delete=models.CASCADE)
    goods_name = models.CharField(max_length=150)
    goods_cover_img = models.CharField(max_length=200)
    selling_price = models.FloatField()
    goods_count = models.IntegerField()
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'order_item'

# 订单地址表
class OrderAddress(models.Model):
    order_address_id = models.IntegerField(primary_key=True, auto_created=True)
    order_nu = models.ForeignKey('MallOrder', on_delete=models.CASCADE)
    user_name = models.CharField(max_length=50)
    user_phone = models.IntegerField(max_length=11)
    province_name = models.CharField(max_length=25)
    city_name = models.CharField(max_length=25)
    region_name = models.CharField(max_length=25)
    detail_address = models.CharField(max_length=100)

    class Meta:
        db_table = 'order_address'

# 首页设置表
# class IndexConfig(models.Model):
#     config_id =
#     config_name
#     config_type
#     goods_id
#     redirect_url
#     config_rank
#     is_deleted
#     create_time
#     create_user
#     update_time
#     update_user

# 评论表
class MallComment(models.Model):
    comment_id = models.IntegerField(primary_key=True, auto_created=True)
    goods_id = models.ForeignKey('GoodsInfo', on_delete=models.CASCADE)
    user_id = models.ForeignKey('MallUser', on_delete=models.CASCADE)
    comment = models.CharField(max_length=250)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'mall_comment'

# 商品收藏表

# 首页宫格图
class GridImg(models.Model):
    grid_id = models.IntegerField(auto_created=True, primary_key=True)
    img_name = models.CharField(max_length=20)
    img_url = models.CharField(max_length=100)
    is_deleted = models.IntegerField(default=0)
    create_time = models.DateField(auto_now_add=True)

    class Meta:
        db_table = 'mall_gridimg'